/**
 * Created by Xiaoming on 2017/9/28.
 */
angular.module('module.scan', [
        'ngStorage',
        'service.mqhp'
    ])

    .config(['$stateProvider',
        function ($stateProvider) {
            $stateProvider
                .state('scan', {
                    url: '/lock?lock_appid&shopid&code&reloadType&devid',
                    templateUrl: scan_viewurl + '/scan.html',
                    resolve: {
                        //wxvisitor:wxvisitor//游客登录操作
                    },
                    controller: 'ScanController',
                    controllerAs: 'vm'
                })

        }])
;//微信游客登录
wxvisitor.$inject = ['$q','$cookies','$state','$stateParams','UserinfoService','WxAuthService','AuthUserService','CupboarlockService'];
function wxvisitor($q,$cookies,$state,$stateParams,UserinfoService,WxAuthService,AuthUserService,CupboarlockService){
    var deferred = $q.defer();
    var appid = $stateParams.lock_appid;
    if (!appid) {
        appid = mall_userauth.wxmp_appid;
    } else {
        mall_userauth.wxmp_appid = appid;
    }
    if (mall_userauth.wxmp_appid != $cookies.get("appid")) {
        //判断不同的公众号进入从新缓存appid,移除用户id缓存
        $cookies.put("appid", mall_userauth.wxmp_appid);
        $cookies.remove("_userId");
    }
    var userid = $cookies.get("_userId");
    console.log(userid);
    if (userid) {
        if(!$stateParams.reloadType){
            var url = window.location.href;
            $cookies.remove("access");
            window.location.href = url + "&reloadType=1";
        }else{
            if($cookies.get("access")){
                setTimeout(function(){
                    $state.go("friendlylink");
                },0)
            }
            else{
                $cookies.put("access","exitAccess");
            }
        }
        console.log("有userid情况");
        //如果有用户获取外卖用户缓存用户身份
        UserinfoService.getxwmUser(userid).$promise.then(function (user) {
            //成功缓存用户信息
            if(user.phone){
                $cookies.put("phone",user.phone);
                //没有头像和用户名的用户去获取用户授权信息
                if (!user.nickname || !user.logo) {
                    var redirect_uri = window.location.href.replace("/lock", "/wxuc");
                    WxAuthService.wxAuthJump(redirect_uri);
                }
            }else{
                $cookies.remove("phone");
            }
            AuthUserService.addUserToCookies(user);
            deferred.resolve(user);
        }, function (error) {
            $cookies.remove("phone");
            deferred.resolve(error);
        });
    }
    //尝试通过code后台登陆
    else if ($stateParams.code) {
        console.log("有code情况");
        if(!$stateParams.reloadType){
            var url = window.location.href;
            $cookies.remove("access");
            window.location.href = url + "&reloadType=1";
        }else{
            if($cookies.get("access")){
                setTimeout(function(){
                    $state.go("friendlylink");
                },0)
            }
            else{
                $cookies.put("access","exitAccess");
            }
        }
        function success_cbfunc(userinfo) {
            //尝试登陆成功缓存用户信息
            UserinfoService.getxwmUser(userinfo.userid).$promise.then(function (user) {
                if(user.phone){
                    $cookies.put("phone",user.phone);
                }else{
                    $cookies.remove("phone");
                }
                //成功缓存用户信息
                AuthUserService.addUserToCookies(user);
                deferred.resolve(user);
            }, function (error) {
                //外卖用户未找到信息
                $cookies.remove("phone");
                UserinfoService.addxwmUser(userinfo).$promise.then(function(){

                },function(){
                    alert("外卖用户添加失败");
                });
                AuthUserService.addUserToCookies(userinfo);
                //成功调起开锁接口
                deferred.resolve(userinfo);
            });
        }
        function error_cbfunc(error) {
            //统一登录不成功注册游客账号
            $cookies.remove("phone");
            //deferred.reject(error);
            var visitor = {
                appid:mall_userauth.wxmp_appid,
                unionid:$cookies.get("openid")
            };
            UserinfoService.wxvisitor(visitor,function(user){
                UserinfoService.addxwmUser(user).$promise.then(function(){

                },function(){
                    alert("外卖用户添加失败");
                });
                //成功缓存用户信息
                AuthUserService.addUserToCookies(user);
                deferred.resolve(user);
            },function(error){
                deferred.resolve(error);
            });
        }
        WxAuthService.wxTryLogin(UserinfoService.accountCheck, success_cbfunc, error_cbfunc);
    } else {
        //不是微信链接地址跳转到微信链接地址
        console.log("不是微信地址跳转微信地址");
        var redirect_uri = window.location.href;
        if(!$stateParams.reloadType){
            $cookies.remove("access");
            redirect_uri += "&reloadType=1";
        }else{
            if($cookies.get("access")){
                $state.go("friendlylink");
            }
            else{
                $cookies.put("access","exitAccess");
            }
        }
        WxAuthService.wxOpenidJump(redirect_uri);
    }
    return deferred.promise;
}